// TableWidget.h
#pragma once

#include <QTableWidget>
#include <QHeaderView>
#include "widgets/bottomWidgetContent/RightWidget.h"
#include "widgets/bottomWidgetContent/QComboBoxManager.h"
#include "config/configtablewidget.h" // 包含 ConfigTableWidget 类


class TableWidget : public RightWidget {
    Q_OBJECT
public:
    explicit TableWidget(QWidget *parent = nullptr)
        : RightWidget(parent)
    {
        // 创建并配置表格控件
        tableWidget = new QTableWidget(this);
        setupTable(tableWidget);

        // 将表格添加到内容布局
        contentLayout()->addWidget(tableWidget);

        // 设置边距
        setMargins(QMargins(0, 0, 0, 0));
    }

protected:
    void initStyles() override {
        RightWidget::initStyles();
        contentWidget()->setStyleSheet("background: white;");
    }

private:
    QTableWidget *tableWidget;
    void setupTable(QTableWidget* table) {
        // 基本配置
        table->setAlternatingRowColors(true);
        table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        table->setEditTriggers(QAbstractItemView::NoEditTriggers);

        updateTableHeaders(table);
        // 设置表格结构
        // const int rowCount = 5;
        // const int columnCount = 3;
        // table->setRowCount(rowCount);
        // table->setColumnCount(columnCount);

        // 设置表头
        // QStringList headers;
        // headers << "ID" << "Name" << "Value";
        // table->setHorizontalHeaderLabels(headers);

        // 填充示例数据
        // for(int row = 0; row < rowCount; ++row) {
        //     table->setItem(row, 0, new QTableWidgetItem(QString::number(row + 1)));
        //     table->setItem(row, 1, new QTableWidgetItem(QString("Item %1").arg(row + 1)));
        //     table->setItem(row, 2, new QTableWidgetItem(QString("%1%").arg((row + 1) * 10)));
        // }

        // 优化显示
        // table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 自动拉伸列宽
        table->verticalHeader()->setVisible(false);    // 隐藏行号
        table->setSelectionBehavior(QAbstractItemView::SelectRows); // 整行选择
        table->setSortingEnabled(true); // 启用排序
    }

    void updateTableHeaders(QTableWidget *tableWidget) {
        QString source = QComboBoxManager::getInstance().getType();

        QString path = "config/" + source + "_config.ini";
        ConfigTableWidget configTableWidget(tableWidget);

        if (source == "BD") {
            configTableWidget.configureTable(path);
        } else if (source == "GPS") {
            configTableWidget.configureTable(path);
        } else if (source == "Galileo") {
            configTableWidget.configureTable(path);
        }
    }
};